<template>
  <div ref="provinceRankingBarChart" style="width: 100%; height: 100%" />
</template>

<script>
import * as echarts from 'echarts'
let chart = null
export default {
  props: {
    data: {
      type: Object,
      default() {
        return {
          areaList: [],
          valueList: []
        }
      }
    }
  },
  methods: {
    initChart() {
      if (null != chart && undefined != chart) {
        chart.dispose()
      }
      chart = this.$echarts.init(this.$refs.provinceRankingBarChart)
      this.setOptions()
    },
    setOptions() {
      let salvProMax = []
      for (let i = 0; i < this.data.valueList.length; i++) {
        salvProMax.push(this.data.valueList[0])
      }
      let option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'none'
          },
          confine: false,
          formatter: function (params) {
            let title = params[0].name
            let name = title + '累计确诊人数: ' + params[0].value + '人'
            return name
          }
        },
        grid: {
          left: '2%',
          right: '2%',
          bottom: '2%',
          top: '2%',
          containLabel: true
        },
        xAxis: {
          show: false,
          type: 'value'
        },
        yAxis: [
          {
            type: 'category',
            axisLabel: {
              textStyle: { fontSize: '80%', color: '#02afff' }
            },
            axisTick: {
              show: false
            },
            splitLine: {
              //网格线
              show: false
            },
            inverse: true, //排序
            axisLine: {
              show: false
            },
            data: this.data.areaList
          },
          {
            type: 'category',
            inverse: true,
            axisTick: 'none',
            axisLine: 'none',
            show: true,
            axisLabel: {
              textStyle: {
                color: '#ffffff',
                fontSize: '12'
              },
              formatter: function (value) {
                return value
              }
            },
            data: this.data.valueList
          }
        ],
        series: [
          {
            name: '值',
            type: 'bar',
            zlevel: 1,
            itemStyle: {
              normal: {
                barBorderRadius: [0, 30, 30, 0],
                color: params => {
                  return new echarts.graphic.LinearGradient(0, 0, 1, 0, [
                    {
                      offset: 0,
                      color: 'rgba(2,163,254,1)'
                    },
                    {
                      offset: 1,
                      color: 'rgba(125,64,255,1)'
                    }
                  ])
                }
              }
            },
            barWidth: 15,
            data: this.data.valueList
          },
          {
            name: '背景',
            type: 'bar',
            barWidth: 15,
            barGap: '-100%',
            data: salvProMax,
            itemStyle: {
              color: '#1B375E',
              barBorderRadius: [0, 30, 30, 0]
            }
          }
        ]
      }
      chart.setOption(option)
    }
  },
  watch: {
    data: {
      handler(newList, oldList) {
        if (oldList != newList) {
          this.setOptions()
        }
      },
      deep: true
    }
  }
}
</script>